from pydantic import BaseModel, Field
from typing import List, Optional
from datetime import datetime
from enum import Enum

class NotificationType(str, Enum):
    INFO = "info"
    WARNING = "warning"
    ERROR = "error"
    SUCCESS = "success"

class NotificationPriority(str, Enum):
    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"

class NotificationBase(BaseModel):
    title: str = Field(..., min_length=1, max_length=200, description="通知标题")
    content: str = Field(..., min_length=1, max_length=1000, description="通知内容")
    type: NotificationType = Field(default=NotificationType.INFO, description="通知类型")
    priority: NotificationPriority = Field(default=NotificationPriority.MEDIUM, description="优先级")
    target_users: Optional[List[str]] = Field(default=[], description="目标用户列表，空表示所有用户")
    expires_at: Optional[datetime] = Field(default=None, description="过期时间")
    is_active: bool = Field(default=True, description="是否激活")

class NotificationCreate(NotificationBase):
    pass

class NotificationUpdate(BaseModel):
    title: Optional[str] = Field(None, min_length=1, max_length=200, description="通知标题")
    content: Optional[str] = Field(None, min_length=1, max_length=1000, description="通知内容")
    type: Optional[NotificationType] = Field(None, description="通知类型")
    priority: Optional[NotificationPriority] = Field(None, description="优先级")
    target_users: Optional[List[str]] = Field(None, description="目标用户列表")
    expires_at: Optional[datetime] = Field(None, description="过期时间")
    is_active: Optional[bool] = Field(None, description="是否激活")

class NotificationInDB(NotificationBase):
    id: str = Field(..., description="通知ID")
    created_by: str = Field(..., description="创建者用户名")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: Optional[datetime] = Field(None, description="更新时间")

class UserNotification(BaseModel):
    user_id: str = Field(..., description="用户ID")
    notification_id: str = Field(..., description="通知ID")
    is_read: bool = Field(default=False, description="是否已读")
    read_at: Optional[datetime] = Field(None, description="阅读时间")
    created_at: datetime = Field(..., description="记录创建时间")

class NotificationResponse(BaseModel):
    id: str
    title: str
    content: str
    type: NotificationType
    priority: NotificationPriority
    target_users: List[str]
    created_by: str
    created_at: datetime
    expires_at: Optional[datetime]
    is_active: bool
    is_read: Optional[bool] = None
    read_at: Optional[datetime] = None

class NotificationListResponse(BaseModel):
    notifications: List[NotificationResponse]
    total: int
    page: int
    size: int

class UnreadCountResponse(BaseModel):
    unread_count: int

class BatchDeleteRequest(BaseModel):
    notification_ids: List[str] = Field(..., min_items=1, description="要删除的通知ID列表")
